package com.GLM.Viewer.Users;

import java.util.Calendar;

import com.GLM.Viewer.R;
import com.GLM.Viewer.Party.CreateNewPartyTimeViewActivity;
import com.GLM.Viewer.Party.CreateNewPartyViewActivity;
import com.GLM.Viewer.Party.PartyDetailActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class UserSettingBirthdayActivity extends Activity implements OnClickListener{
	private Calendar calendar;
	Button saveButton;
	Button gobackButton;
	private String birthday;
	TextView birthdayView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_userdetail_birthday);
		saveButton = (Button)findViewById(R.id.ftop_save_button);
		gobackButton = (Button)findViewById(R.id.goback_button);
		birthdayView = (TextView)findViewById(R.id.birthday_selected);
		DatePicker dp = (DatePicker) findViewById(R.id.birthday_datepicker);
		
		calendar = Calendar.getInstance();
		Intent intent = getIntent();
		birthday = intent.getStringExtra("birthday");
		Log.d("UserBirthday", birthday+"");
		if(birthday != null && !birthday.equals("")){
			String[] dates = birthday.split(" ");
			
			String date = String.format(getString(R.string.setting_birthday_text), dates[2],dates[0],dates[1]);
			birthdayView.setText(date);
		}
		else{
			birthdayView.setText(getString(R.string.setting_unselect_birthday));
		}
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		
		dp.init(year, month, day, new DatePicker.OnDateChangedListener() {
			
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				calendar.set(Calendar.YEAR, year);
				calendar.set(Calendar.MONTH, monthOfYear);
				calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				birthdayView.setText(String.format(getString(R.string.setting_birthday_text), year,monthOfYear+1,dayOfMonth));
			}
		});
		
		gobackButton.setOnClickListener(this);
		saveButton.setOnClickListener(this);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == saveButton){
			DateFormat format = new DateFormat();
		    birthday = (String) format.format("yyyy-MM-dd", calendar);
		    Log.d("formated", birthday);
		    Intent intent = new Intent(UserSettingBirthdayActivity.this,UserSettingActivity.class);
		    intent.putExtra("birthday", birthday);
		    setResult(RESULT_OK, intent);
		    finish();
		}
		else if(v == gobackButton){
			
			finish();
		}
	}
}
